package com.saohuobang.test.model.enums;

import java.util.Objects;

/**
 * 作者 yaohua.liu
 * 日期 2015-11-05 09:20
 * 说明  http response content type
 */
public enum ContentTypeEnum {
    TEXT_PLAIN(1, "text", "text/plain"),
    APPLICATION_XML(2, "xml", "application/xml"),
    APPLICATION_JSON(3, "json", "application/json"),
    TEXT_HTML(4, "html", "text/html");

    private int code;
    private String name;
    private String desc;


    ContentTypeEnum(int code, String name, String desc) {
        this.code = code;
        this.name = name;
        this.desc = desc;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static String getDescByName(String name) {
        for (ContentTypeEnum entry : values()) {
            if (Objects.equals(entry.getName(), name)) {
                return entry.desc;
            }
        }
        return TEXT_PLAIN.desc;
    }

    public static ContentTypeEnum codeOf(int code) {
        for (ContentTypeEnum entry : values()) {
            if (entry.getCode() == code) {
                return entry;
            }
        }
        return null;
    }
}
